﻿
/***更新****************************************************************   
*       最后更新 : ZhangJunhua   
*       更新日期 : 2010/04/15   
*       更新目的 : 增加  功能；修复bug   
*       更新版本 : 1.00   
*       更新日志 : 日志   
************************************************************************/     
  
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace System.MathTools
{
    public class MathUtil
    {
        public static double arraySquareSum(double[] ad)
        {
            try
            {
                double d = 0.0;
                for (int i = 0; i < ad.Length; i++)
                    d += Math.Pow(ad[i], 2);

                return d;
            }
            catch (Exception numberformatexception)
            {
                throw numberformatexception;
            }
        }

        public static double arraySum(double[] ad)
        {
            try
            {
                double d = 0.0;
                for(int i = 0; i < ad.Length; i++)
                    d += ad[i];

                return d;
            }
            catch(Exception numberformatexception)
            {
                throw numberformatexception;
            }
        }

        public static double arraySum(double[] ad, double[] ad1)
        {
            try
            {
                double d = 0.0;
                if (ad.Length == ad1.Length)
                {
                    for (int i = 0; i < ad.Length; i++)
                        d += ad[i] * ad1[i];

                    return d;
                }
                else
                {
                    return 0.0;
                }
            }
            catch (Exception numberformatexception)
            {
                throw numberformatexception;
            }
        }

        public static double doubleCut(double d, int i)
        {
            try
            {
                double d1 = 0.0;
                int j = 0;
                if (Math.Abs(d) >= 1.0)
                {
                    for (int k = 1; k <= 1000; k++)
                    {
                        d1 = (int)(d / Math.Pow(10, k));
                        if (d1 != 0.0)
                            j++;
                        else
                            k = 1001;
                    }

                    if (i - j < 0)
                        d1 = (double)Math.Round(d / Math.Pow(10, j - i)) * Math.Pow(10, j - i);
                    else
                        d1 = (double)Math.Round(d * Math.Pow(10, i - j)) / Math.Pow(10, i - j);
                }
                else
                {
                    for (int l = 1; l <= 1000; l++)
                    {
                        d1 = (int)(d * Math.Pow(10, l));
                        if (d1 == 0.0)
                            j++;
                        else
                            l = 1001;
                    }

                    d1 = (double)Math.Round(d * Math.Pow(10, j + i)) / Math.Pow(10, j + i);
                }
                return d1;
            }
            catch (Exception numberformatexception)
            {
                throw numberformatexception;
            }
        }

        public static double[] sort(double[] ad)
        {
            try
            {
                for (int i = 0; i < ad.Length; i++)
                {
                    for (int j = 0; j < ad.Length - 1; j++)
                        if (ad[j] > ad[j + 1])
                        {
                            double d1 = ad[j];
                            ad[j] = ad[j + 1];
                            ad[j + 1] = d1;
                        }

                }

                return ad;
            }
            catch (Exception numberformatexception)
            {
                throw numberformatexception;
            }
        }
    }
}
